#pragma once

inline int GetBlockCount(int nItems)
{
	switch(nItems)
	{
	case 2:	 return 2;
	case 3: 
	case 4:  return 4;
	case 5:
	case 6:  return 6;
	case 7:
	case 8:
	case 9:  return 9;
	default: return 1;
	}
}

inline void GetRowCol(int& nRows, int& nCols, int nBlockCount)
{
	switch(nBlockCount)
	{
	case 2:	 nRows = 1; nCols = 2; break;
	case 4:  nRows = 2; nCols = 2; break;
	case 6:  nRows = 2; nCols = 3; break;
	case 9:  nRows = 3; nCols = 3; break;
	default: nRows = 1; nCols = 1; break;
	}
}

static void GetBlockRect(std::vector<CRect>& vecRectBlocks, int nItems, CRect rcClient, bool bHorizontal = false)
{
	int nBlockCount = GetBlockCount(nItems);

	if(nBlockCount == 1)
	{
		vecRectBlocks.push_back(rcClient);
		return;
	}

	int nRows = 0;
	int nCols = 0;
	GetRowCol(nRows, nCols, nBlockCount);

	if(nBlockCount == 2 && bHorizontal)
		std::swap(nRows, nCols);

	int dx = rcClient.Width() / nCols;
	int dy = rcClient.Height() / nRows;

	vecRectBlocks.resize(nRows * nCols);
	for(int i = 0; i < nRows; ++i)
	{
		for(int j = 0; j < nCols; ++j)
		{
			CRect& rc = vecRectBlocks[i * nCols + j];
			rc.SetRect(j * dx, i * dy, (j + 1) * dx, (i + 1) * dy);
		}
	}
}
